package com.lan.utils.image;

import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;

import java.io.File;

/**
 * 图像识别 Util
 *
 * @author shadow
 * @since 2022-09-23
 */
public class OCRUtil {

    //训练库的位置
    private static final String DATA_PATH = "E:\\MyWorkspace\\student\\student-sys\\tessdata";
    //语言 chi_sim--简体中文  eng--默认英文
    private static final String LANG = "chi_sim";

    /**
     * ocr.
     *
     * */
    public static String ocr(final String filePath) throws TesseractException {
        ITesseract tesseract = new Tesseract();
        tesseract.setDatapath(DATA_PATH);
        tesseract.setLanguage(LANG);
        File imageFile = new File(filePath);
        String ocrResult = tesseract.doOCR(imageFile);
        ocrResult.replace("\n\n", "");
        return ocrResult;
    }
}
